package com.dh.leetcode.zero;

import org.junit.Assert;
import org.junit.Test;

/**
 * @ClassName: _33_search_in_rotated_sorted_array
 * @Description: 33. 搜索旋转排序数组
 * 升序排列的整数数组 nums 在预先未知的某个点上进行了旋转（例如， [0,1,2,4,5,6,7] 经旋转后可能变为 [4,5,6,7,0,1,2] ）。
 * <p>
 * 请你在数组中搜索 target ，如果数组中存在这个目标值，则返回它的索引，否则返回 -1 。
 * <p>
 * <p>
 * <p>
 * 示例 1：
 * <p>
 * 输入：nums = [4,5,6,7,0,1,2], target = 0
 * 输出：4
 * 示例 2：
 * <p>
 * 输入：nums = [4,5,6,7,0,1,2], target = 3
 * 输出：-1
 * 示例 3：
 * <p>
 * 输入：nums = [1], target = 0
 * 输出：-1
 * <p>
 * <p>
 * 提示：
 * <p>
 * 1 <= nums.length <= 5000
 * -10^4 <= nums[i] <= 10^4
 * nums 中的每个值都 独一无二
 * nums 肯定会在某个点上旋转
 * -10^4 <= target <= 10^4
 * <p>
 * https://leetcode-cn.com/problems/search-in-rotated-sorted-array/
 * @Author: shouzimu
 * @Date: 2020/12/25 16:18
 */
public class _33_search_in_rotated_sorted_array {

    /**
     * 二分查找的变种
     * <p>
     * 1、二分找到翻转点
     * 2、二分查找
     *
     * 辣鸡写法，提交错了6次，卧槽勒，心态崩了
     *
     * @param nums
     * @param target
     * @return
     */
    public int search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        if (r < 5) {
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == target) {
                    return i;
                }
            }
            return -1;
        }
        int first = nums[0];
        int last = nums[r];
        int mid = 0;
        while (l <= r) {
            mid = l + (r - l) / 2;
            if (mid == 0) {
                mid++;
                break;
            }
            if (nums[mid] < nums[mid - 1] && (mid + 1 == nums.length ? true : nums[mid] < nums[mid + 1])) {
                break;
            }
            if (nums[mid] > first) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        if (first <= target && target <= nums[mid - 1]) {
            l = 0;
            r = mid - 1;
        }

        if (nums[mid] <= target && target <= last) {
            l = mid;
            r = nums.length - 1;
        }

        while (l <= r) {
            mid = l + (r - l) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] < target) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return -1;
    }

    @Test
    public void searchTest() {
        Assert.assertEquals(3, search(new int[]{6, 1, 2, 3, 4, 5}, 3));
        Assert.assertEquals(1, search(new int[]{2, 3, 4, 5, 6, 7, 8, 1}, 3));
        Assert.assertEquals(6, search(new int[]{4, 5, 6, 7, 0, 1, 2}, 2));
        Assert.assertEquals(1, search(new int[]{1, 3}, 3));
        Assert.assertEquals(4, search(new int[]{4, 5, 6, 7, 8, 1, 2, 3}, 8));
        Assert.assertEquals(3, search(new int[]{7, 1, 2, 3, 4, 5, 6}, 3));
        Assert.assertEquals(4, search(new int[]{4, 5, 6, 7, 0, 1, 2}, 0));
        Assert.assertEquals(-1, search(new int[]{4, 5, 6, 7, 0, 1, 2}, 3));
        Assert.assertEquals(-1, search(new int[]{1}, 0));
        Assert.assertEquals(5, search(new int[]{6, 7, 8, 9, 10, 1, 2, 3, 4, 5}, 1));
        Assert.assertEquals(4, search(new int[]{6, 7, 8, 9, 10, 1, 2, 3, 4, 5}, 10));
    }
}
